package com.payment.controller;
import com.payment.entity.Patients;
import com.payment.service.PatientsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/patients") //患者接口
public class PatientsController {

    @Resource
    private  PatientsService patientsService; //注入服务

    //登录
    @PostMapping("/login")
    public ResponseEntity<ResDTO> login(@RequestBody Patients entity){
        Patients res=patientsService.login(entity);
        System.out.println(res);

        if (res!=null){
            return ResponseEntity.ok(ResDTO.success(res));
        }else{
            return ResponseEntity.ok(ResDTO.error("找不到该用户！"));
        }
    }

    //查询全部患者数据
    @GetMapping("/getAllUser")
    public ResponseEntity<ResDTO>getAll(){
        List<Patients> _list = patientsService.getAll();

        System.out.println("////////////////////////");
        System.out.println(_list);
        List<Patients> res = new ArrayList<>();
        if (_list!=null){
            for (Patients user : _list) {
                if (user.getIsDelActive() == 0) { // 只添加未删除的用户
                    res.add(user);
                }
            }
            return ResponseEntity.ok(ResDTO.success(res));
        }
        return ResponseEntity.ok(ResDTO.error());
    }

    //新增
    @PostMapping("/addUser")
    public ResponseEntity<ResDTO> add(@RequestBody Patients entity){

        int res= patientsService.add(entity);

        if (res>0){
            return  ResponseEntity.ok(ResDTO.success(null));
        }else{
            return  ResponseEntity.ok(ResDTO.error());
        }

    }

    //修改  需要id和对象（要修改的对象）
    @PutMapping("/putUser/{id}")
    public ResponseEntity<ResDTO> update(@PathVariable int id,@RequestBody Patients entity){
        Patients table  = patientsService.getById(id);
        if (table!=null){
            table.setUserName(entity.getUserName());
            table.setUserPasswd(entity.getUserPasswd());
            table.setNickName(entity.getNickName());
            table.setTelphone(entity.getTelphone());
            patientsService.update(table);
            return ResponseEntity.ok(ResDTO.success(table));
        }else {
            return ResponseEntity.ok(ResDTO.error("更新失败！"));
        }
    }
    //删除
    @DeleteMapping("/delUser/{id}")
    public ResponseEntity<ResDTO> remove(@PathVariable int id){
        int res= patientsService.del(id);
        if (res!=0){
            return ResponseEntity.ok(ResDTO.success(null));
        }else {
            return ResponseEntity.ok(ResDTO.error("没有找到该用户！删除失败！"));
        }
    }
}
